<?php
class CreatehtmlAction extends AdminbaseAction {
	
    protected  $module;
    public function _initialize()
    {
        parent::_initialize(); 
        foreach ((array)$this->module as $rw){
			if($rw['type']==1  && $rw['status']==1)  $data['module'][$rw['id']] = $rw;
        }
		
		$this->module=$data['module'];
		$this->assign('module',$this->module);
    }

    public function index()
    {
		$this->display('Admin/Createhtml_index');
    }

	public function createindex()
	{	
		$this->create_index();
		$this->assign ( 'jumpUrl', U(MODULE_NAME.'/index') );
		$this->success(L('index_create_OK'));		 
	}

 	public function createlist($_GET)
    {
		if(isset($_GET['dosubmit'])) {
			$this->assign ( 'waitSecond', 2);
			extract($_GET,EXTR_SKIP);
			$moduleid = intval($_GET['moduleid']);
			$doid = $doid ? intval($doid) : 0;

			if($dosubmit!=1){
				if($catids[0] == 0) { 
					$catids=array();
					foreach($this->categorys as $id=>$cat) {
						if($cat['type']!=0  || $cat['ishtml']!=1) continue;
						if($moduleid){									
							if($cat['moduleid']!=$moduleid) continue;
						}
						$catids[] = $id;
					}
				}
				$_SESSION['catids'] = $catids;
			}else{
				$catids =$_SESSION['catids'];	
			}

	
				if(!isset($catids[$doid])){
					unset($_SESSION['catids']);
					$forward = U("Createhtml/".ACTION_NAME);
					$this->assign ( 'jumpUrl', $forward);
					$this->success(L('create_update_success'));
				}else{
					$id = $catids[$doid];		
					$p = max(intval($p), 1);
					$j = 1;
					do {
						$this->create_list($id,$p);					
						$j++;
						$p++;
						$pages = isset($pages) ? $pages : PAGESTOTAL;
						 
					} while ($j <= $pages && $j < $pagesize);

					if($p <= $pages)  {
						$endpage = intval($p+$pagesize);
						$percent = round($p/$pages, 2)*100;
						$urlarray=array(
							'doid' => $doid,
							'dosubmit' => 1,
							'pages' => $pages,
							'p' => $p,
							'pagesize' => $pagesize,
						);						
						$message = L('updating').$this->categorys[$id]['catname'].L('create_update_count').$pages.L('create_update_list_num').$p.L('items_list').$percent.L('items1');
						$forward = U("Createhtml/".ACTION_NAME,$urlarray);
					} else {
						$doid++;
						$urlarray=array(
							'doid' => $doid,
							'dosubmit' => 1,
							'p' => 1,
							'pagesize' => $pagesize,
						);
						$message = L('start_updating').$this->categorys[$id]['catname']." ...";
						$forward = U("Createhtml/".ACTION_NAME,$urlarray);						
					}
					$this->assign ( 'jumpUrl', $forward);
					$this->success($message);
				}
			
		}else{
 
			if($this->categorys){
				foreach ($this->categorys as $r){
					if($r['type']==1 && $r['ishtml']==0) continue;
					if($_GET['moduleid'] && $r['moduleid'] !=  $_GET['moduleid']) continue;
					if(ACTION_NAME=='Updateurl' && $r['module']=='Page') continue;
					if(ACTION_NAME=='Createlist' && $r['ishtml']!=1) continue;
					if((ACTION_NAME=='Createshow' && $r['ishtml']!=1) || (ACTION_NAME=='Createshow' && $r['module']=='Page')) continue;				
					if($r['child'] && ACTION_NAME!='Createlist'){ 
						$r['disabled'] = 'disabled';
					}else{
						$r['disabled'] = '';
					}
					$array[] = $r;
				}
				import ( '@.ORG.Tree' );	
				$str  = "<option value='\$id'  \$disabled>\$spacer \$catname</option>";
				$tree = new Tree ($array);	
				$tree->icon = array('&nbsp;&nbsp;&nbsp;'.L('tree_1'),'&nbsp;&nbsp;&nbsp;'.L('tree_2'),'&nbsp;&nbsp;&nbsp;'.L('tree_3'));
				$select_categorys = $tree->get_tree(0, $str);
				$this->assign('select_categorys', $select_categorys);
			}

			$this->display('Admin/Createhtml_show');	 
		}
    }

	public function updateurl()
    {
	 
		if(isset($_GET['dosubmit'])) {
			$this->assign ( 'waitSecond', 2);
			extract($_GET,EXTR_SKIP);
			$moduleid = intval($_GET['moduleid']);
			if($moduleid){
				$module=$this->module[$moduleid]['name'];
				$dao = M($module);

				$p = max(intval($p), 1);
				$start = $pagesize*($p-1);

				if(is_array($catids) && $catids[0] > 0){
					$catids = implode(',',$catids);
					$where = " catid IN($catids) ";
					$_SESSION['catids'] = $catids;					
				}
				if($_SESSION['catids']){
					$catids = $_SESSION['catids'];
					$where = " catid IN($catids) ";
				}
				if(!isset($count)){
					$count = $dao->where($where)->count();
				}
				$pages = ceil($count/$pagesize);
				
				if($count){
					$list = $dao->field('id,catid,url')->where($where)->limit($start . ',' . $pagesize)->select();				 
					foreach($list as $r) {
						if($r['islink']) continue;
						$url = geturl($this->categorys[$r['catid']],$r['id'],$this->sysConfig);
						unset($r['catid']);
						$r['url'] = $url['0'];
						$dao->save($r);
					}					 
				}

				if($pages > $p) {
					$p++;
					$creatednum = $start + count($list);
					$percent = round($creatednum/$count, 2)*100;
					$urlarray=array(
						'moduleid' => $moduleid,
						'dosubmit' => 1,
						'count' => $count,
						'pages' => $pages,
						'p' => $p,
						'pagesize' => $pagesize,
					);
					 
					$message = L('create_update_count').$count.L('create_update_num').$creatednum.L('items').$percent.L('items1');
					$forward = U("Createhtml/Showurl",$urlarray);
					$this->assign ( 'jumpUrl', $forward);					
					$this->success($message);
				} else {
					unset($_SESSION['catids']);
					$forward = U("Createhtml/Showurl");
					$this->assign ( 'jumpUrl', $forward);
					$this->success(L('create_update_success'));
				}

			}else{
				//按照栏目更新url
				extract($_GET,EXTR_SKIP);
				$doid = $doid ? intval($doid) : 0;
				if(empty($_SESSION['catids']) && $catids){
					if($catids[0] == 0) { 
							foreach($this->categorys as $id=>$cat) {
								if($cat['child'] || $cat['type']!=0 || $cat['module']=='Page') continue;
								$catids[] = $id;
							}
					}
					$_SESSION['catids'] = $catids;
				}else{
					$catids =$_SESSION['catids'];				
				}
				if(!isset($catids[$doid])){
					unset($_SESSION['catids']);
					$forward = U("Createhtml/".ACTION_NAME);
					$this->assign ( 'jumpUrl', $forward);
					$this->success(L('create_update_success'));
				}else{
					$id = $catids[$doid];
					$module=$this->categorys[$id]['module'];
					$dao = M($module);
					$where = "catid=$id";
					$p = max(intval($p), 1);
					$start = $pagesize*($p-1);

					if(!isset($count)){
						$count = $dao->where($where)->count();
					}
					$pages = ceil($count/$pagesize);
					
					if($count){
						$list = $dao->field('id,catid,url')->where($where)->limit($start . ',' . $pagesize)->select();				 
						foreach($list as $r) {
							if($r['islink']) continue;
							$url = geturl($this->categorys[$r['catid']],$r['id'],$this->sysConfig);
							unset($r['catid']);
							$r['url'] = $url['0'];
							$dao->save($r);
						}
					}
 
					if($pages > $p) {
						$p++;
						$creatednum = $start + count($list);
						$percent = round($creatednum/$count, 2)*100;
						$urlarray=array(
							'doid' => $doid,
							'dosubmit' => 1,
							'count' => $count,
							'pages' => $pages,
							'p' => $p,
							'pagesize' => $pagesize,
						);
						 
						$message = L('updating').$this->categorys[$id]['catname'].L('create_update_count').$count.L('create_update_num').$creatednum.L('items').$percent.L('items1');
						$forward = U("Createhtml/".ACTION_NAME,$urlarray);
						$this->assign ( 'jumpUrl', $forward);
						$this->success($message);
					} else {
						$doid++;
						$urlarray=array(
							'doid' => $doid,
							'dosubmit' => 1,
							'p' => 1,
							'pagesize' => $pagesize,
						);
						$message = L('start_updating').$this->categorys[$id]['catname']." ...";
						$forward = U("Createhtml/".ACTION_NAME,$urlarray);
						$this->assign ( 'jumpUrl', $forward);
						$this->success($message);
					}
				}
			}
		}else{				
			if($this->categorys){
				foreach ($this->categorys as $r){
					if($r['type']==1 && $r['ishtml']==0) continue;
					if($_GET['moduleid'] && $r['moduleid'] !=  $_GET['moduleid']) continue;
					if(ACTION_NAME=='Updateurl' && $r['module']=='Page') continue;
					if(ACTION_NAME=='Createlist' && $r['ishtml']!=1) continue;
					if((ACTION_NAME=='Createshow' && $r['ishtml']!=1) || (ACTION_NAME=='Createshow' && $r['module']=='Page')) continue;				
					if($r['child'] && ACTION_NAME!='Createlist'){ 
						$r['disabled'] = 'disabled';
					}else{
						$r['disabled'] = '';
					}
					$array[] = $r;
				}
				import ( '@.ORG.Tree' );	
				$str  = "<option value='\$id'  \$disabled>\$spacer \$catname</option>";
				$tree = new Tree ($array);	
				$tree->icon = array('&nbsp;&nbsp;&nbsp;'.L('tree_1'),'&nbsp;&nbsp;&nbsp;'.L('tree_2'),'&nbsp;&nbsp;&nbsp;'.L('tree_3'));
				$select_categorys = $tree->get_tree(0, $str);
				$this->assign('select_categorys', $select_categorys);
			}
			$this->display('Admin/Createhtml_show');	 
		} 
    }

	public function createshow($_GET)
    {
		if(isset($_GET['dosubmit'])) {
			$this->assign ( 'waitSecond', 2);
			extract($_GET,EXTR_SKIP);
			$moduleid = intval($_GET['moduleid']);
			$doid = $doid ? intval($doid) : 0;

			if($dosubmit!=1){
				if($catids[0] == 0) { 
					$catids=array();
					foreach($this->categorys as $id=>$cat) {
						if($cat['child'] || $cat['type']!=0 || $cat['module']=='Page' || $cat['ishtml']!=1) continue;
						if($moduleid){									
							if($cat['moduleid']!=$moduleid) continue;
						}
						$catids[] = $id;
					}
				}	
				$_SESSION['catids'] = $catids;
			}else{
				$catids =$_SESSION['catids'];	
			}
				if(!isset($catids[$doid])){
					unset($_SESSION['catids']);
					$forward = U("Createhtml/".ACTION_NAME);
					$this->assign ( 'jumpUrl', $forward);
					$this->success(L('create_update_success'));
				}else{
					$id = $catids[$doid];
					$module=$this->categorys[$id]['module'];
					$dao = M($module);
					$where = "catid=$id";
					$p = max(intval($p), 1);
					$start = $pagesize*($p-1);

					if(!isset($count)){
						$count = $dao->where($where)->count();
					}
					$pages = ceil($count/$pagesize);
					
					if($count){
						$list = $dao->field('id,catid,url')->where($where)->limit($start . ',' . $pagesize)->select();				 
						foreach($list as $r) {
							if($r['islink']) continue;
							$module = $this->categorys[$r['catid']]['module'];
							$this->create_show($r['id'],$module);
						}
					}

					if($pages > $p) {
						$p++;
						$creatednum = $start + count($list);
						$percent = round($creatednum/$count, 2)*100;
						$urlarray=array(
							'doid' => $doid,
							'dosubmit' => 1,
							'count' => $count,
							'pages' => $pages,
							'p' => $p,
							'pagesize' => $pagesize,
						);
						 
						$message = L('updating').$this->categorys[$id]['catname'].L('create_update_count').$count.L('create_update_num').$creatednum.L('items').$percent.L('items1');
						$forward = U("Createhtml/".ACTION_NAME,$urlarray);
						$this->assign ( 'jumpUrl', $forward);
						$this->success($message);
					} else {
						$doid++;
						$urlarray=array(
							'doid' => $doid,
							'dosubmit' => 1,
							'p' => 1,
							'pagesize' => $pagesize,
						);
						$message = L('start_updating').$this->categorys[$id]['catname']." ...";
						$forward = U("Createhtml/".ACTION_NAME,$urlarray);
						$this->assign ( 'jumpUrl', $forward);
						$this->success($message);
					}
				}
		}else{
			if($this->categorys){
				foreach ($this->categorys as $r){
					if($r['type']==1 && $r['ishtml']==0) continue;
					if($_GET['moduleid'] && $r['moduleid'] !=  $_GET['moduleid']) continue;
					if(ACTION_NAME=='Updateurl' && $r['module']=='Page') continue;
					if(ACTION_NAME=='Createlist' && $r['ishtml']!=1) continue;
					if((ACTION_NAME=='Createshow' && $r['ishtml']!=1) || (ACTION_NAME=='Createshow' && $r['module']=='Page')) continue;				
					if($r['child'] && ACTION_NAME!='Createlist'){ 
						$r['disabled'] = 'disabled';
					}else{
						$r['disabled'] = '';
					}
					$array[] = $r;
				}
				import ( '@.ORG.Tree' );	
				$str  = "<option value='\$id'  \$disabled>\$spacer \$catname</option>";
				$tree = new Tree ($array);	
				$tree->icon = array('&nbsp;&nbsp;&nbsp;'.L('tree_1'),'&nbsp;&nbsp;&nbsp;'.L('tree_2'),'&nbsp;&nbsp;&nbsp;'.L('tree_3'));
				$select_categorys = $tree->get_tree(0, $str);
				$this->assign('select_categorys', $select_categorys);
			}

			$this->display('Admin/Createhtml_show');	
		}
	}



	public function createdetail(){
		
		if(isset($_POST['dosubmit'])) {
			if($_POST['t']){
				C('HTML_FILE_SUFFIX','.html');
				if(false === strripos($_POST['t'],'.')){$t=$_POST['t'];}else{$tarr = split ('[.]', $_POST['t']);$t=$tarr[0];}
				if(!empty($_POST['b'])){$map['brand']=trim($_POST['b']);}
				$M=M('productadmin');
				$v=$M->where($map)->select();
				$limitstr=array(
					array(array('\\',':','*','?','"','<','>','|',' '),'-'),
					array(array('/'),'_'),
				);
				$cii=0;

				set_time_limit (0);
				foreach($v as $val){
					$this->assign('product', $val);

$currtitle=$val['title'];
$currbattery=$val['battery'];
					$val['title']=str_replace("，",",",$val['title']);
					$val['battery']=str_replace("，",",",$val['battery']);
					$val['productcode']=str_replace("，",",",$val['productcode']);
					$val['capacity']=str_replace("，",",",$val['capacity']);
					$val['voltage']=str_replace("，",",",$val['voltage']);
					$val['dollarpreferential']=str_replace("，",",",$val['dollarpreferential']);
					$val['dollarmarket']=str_replace("，",",",$val['dollarmarket']);

					$titlearr = split ('[,]', $val['title']);
					$batteryarr = split ('[,]', $val['battery']);




$_titlearr=array();
$_batteryarr=array();
foreach($titlearr as $_v){
	$_v=trim($_v);
	if(!empty($_v))$_titlearr[]=$_v;
}
foreach($batteryarr as $_v){
	$_v=trim($_v);
	if(!empty($_v))$_batteryarr[]=$_v;
}
$titlearr =$_titlearr;
$batteryarr =$_batteryarr;
unset($_titlearr);unset($_batteryarr);



					$productcodearr = split ('[,]', $val['productcode']);
					$capacityarr = split ('[,]', $val['capacity']);
					$voltagearr = split ('[,]', $val['voltage']);
					$pricearr = split ('[,]', $val['dollarpreferential']);
					$picsarr = split ('[,]', $val['dollarmarket']);
					$sizearr = split ('[,]', $val['size']);
					$colorarr = split ('[,]', $val['color']);
					$weightarr = split ('[,]', $val['weight']);






if(count($capacityarr)!=count($productcodearr)){
	$_val=$productcodearr[count($productcodearr)-1];
	foreach($capacityarr as $k=>$val){
		$productcodearr[$k]=$_val;
	}
}
if(count($capacityarr)!=count($voltagearr)){
	$_val=$voltagearr[count($voltagearr)-1];
	foreach($capacityarr as $k=>$val){
		$voltagearr[$k]=$_val;
	}
}
if(count($capacityarr)!=count($pricearr)){
	$_val=$pricearr[count($pricearr)-1];
	foreach($capacityarr as $k=>$val){
		$pricearr[$k]=$_val;
	}
}
if(count($capacityarr)!=count($picsarr)){
	$_val=$picsarr[count($picsarr)-1];
	foreach($capacityarr as $k=>$val){
		$picsarr[$k]=$_val;
	}
}
if(count($capacityarr)!=count($sizearr)){
	$_val=$sizearr[count($sizearr)-1];
	foreach($capacityarr as $k=>$val){
		$sizearr[$k]=$_val;
	}
}
if(count($capacityarr)!=count($colorarr)){
	$_val=$colorarr[count($colorarr)-1];
	foreach($capacityarr as $k=>$val){
		$colorarr[$k]=$_val;
	}
}
if(count($capacityarr)!=count($weightarr)){
	$_val=$weightarr[count($weightarr)-1];
	foreach($capacityarr as $k=>$val){
		$weightarr[$k]=$_val;
	}
}
foreach($capacityarr as $k=>$val){
	$capacityarr[$k]=array('title'=>$val,'pic'=>$picsarr[$k],'price'=>$pricearr[$k],'voltage'=>$voltagearr[$k],'productcode'=>$productcodearr[$k],'size'=>$sizearr[$k],'color'=>$colorarr[$k],'weight'=>$weightarr[$k]);
}

					$this->assign('capacity_arr', $capacityarr);

					$i=0;
					foreach($titlearr as $vv){
						$vv_low=strtolower($vv);
						$vv_filename=strtolower(trim($vv));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}

						$series_arr[$i]['title']=$vv;
						$series_arr[$i]['title1']=$vv_low;
						$series_arr[$i]['filename']=$vv_filename;
						unset($vv_low);unset($vv_filename);
						$i++;
					}



					$i=0;
					foreach($batteryarr as $vv){
						$vv_low=strtolower($vv);
						$vv_filename=strtolower(trim($vv));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}

						$battery_arr[$i]['title']=$vv;
						$battery_arr[$i]['title1']=$vv_low;
						$battery_arr[$i]['filename']=$vv_filename;
						unset($vv_low);unset($vv_filename);
						$i++;
					}


					$this->assign('series_arr', $series_arr);
					$this->assign('battery_arr', $battery_arr);


					foreach($titlearr as $vv){

						$vv_low=strtolower($vv);
						$vv_filename=strtolower(trim($vv));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}
						$this->assign('title', $vv);
						$this->assign('title1', $vv_low);
						$this->assign('filename', $vv_filename);
//						echo $cii.':'.$vv_filename.'<br />';
if(is_file('./Html/'.$vv_filename.'.html')){echo 'file existed:<a target="_blank" href="./Html/'.$vv_filename.'.html">'.$cii.':'.$vv_filename.'.html</a><br />';
echo 'series:'.$currtitle.'<br /><br />';
}
						$s=$this->buildHtml($vv_filename,'./Html/','Home/'.$t);
if(!is_file('./Html/'.$vv_filename.'.html')){echo 'nofile:<a target="_blank" href="./Html/'.$vv_filename.'.html">'.$cii.':'.$vv_filename.'.html</a><br />';
echo 'series:'.$currtitle.'<br /><br />';
}

unset($vv_low);unset($vv_filename);
					}

					foreach($batteryarr as $vv){

						$vv_low=strtolower($vv);
						$vv_filename=strtolower(trim($vv));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}
						$this->assign('title', $vv);
						$this->assign('title1', $vv_low);
						$this->assign('filename', $vv_filename);
//						echo $cii.':'.$vv_filename.'<br />';
if(is_file('./Html/'.$vv_filename.'.html')){echo 'file existed:<a target="_blank" href="./Html/'.$vv_filename.'.html">'.$cii.':'.$vv_filename.'.html</a><br />';
echo 'battery:'.$currbattery.'<br /><br />';
}
						$this->buildHtml($vv_filename,'./Html/','Home/'.$t);
if(!is_file('./Html/'.$vv_filename.'.html')){echo 'nofile:<a target="_blank" href="./Html/'.$vv_filename.'.html">'.$cii.':'.$vv_filename.'.html</a><br />';
echo 'battery:'.$currbattery.'<br /><br />';
}

unset($vv_low);unset($vv_filename);
					}

unset($series_arr);unset($battery_arr);
				}

			}

			$this->ajaxReturn('ok');
		}else{
			$type=  'html';
			$path='./Yourphp/Tpl/Default/Home/';;
			$files = dir_list($path,$type);
			foreach ($files as $key=>$file){
				$filename = basename($file);
				$templates[$key]['filename'] = $filename;
			}
			$this->assign ( 'templates',$templates );
			$M=M('productadmin');
			$count['re']=$M->count();
			$count['total']=0;$count['content']=0;
			$v=$M->field('title,battery,capacity,brand')->select();
			foreach($v as $k=>$val){
				$val['title']=str_replace("，",",",$val['title']);
				$val['battery']=str_replace("，",",",$val['battery']);
				$titlearr = split ('[,]', trim($val['title']));
				$batteryarr = split ('[,]', trim($val['battery']));

$_titlearr=array();
$_batteryarr=array();
foreach($titlearr as $_v){
	$_v=trim($_v);
	if(!empty($_v))$_titlearr[]=$_v;
}
foreach($batteryarr as $_v){
	$_v=trim($_v);
	if(!empty($_v))$_batteryarr[]=$_v;
}
$titlearr =$_titlearr;
$batteryarr =$_batteryarr;
unset($_titlearr);unset($_batteryarr);
				$count['total']=$count['total']+count($titlearr)+count($batteryarr);
				$count['content']=$count['content']+','+$val['title']+','+$val['battery']+',';
				$b[$k]=strtolower($val['brand']);
			}
			$bb=array_unique($b);

			$_bb=array();
			foreach($bb as $k=>$val){
				$_bb[$k]['re']=$M->where('brand="'.$val.'"')->count();
				$_bb[$k]['total']=0;
				$_bb[$k]['content']=0;
				$_bb[$k]['txt']=$val;
				$v=null;
				$v=$M->field('title,battery')->where('brand="'.$val.'"')->select();
				foreach($v as $k2=>$val2){
					$val2['title']=str_replace("，",",",$val2['title']);
					$val2['battery']=str_replace("，",",",$val2['battery']);
					$titlearr = split ('[,]', trim($val2['title']));
					$batteryarr = split ('[,]', trim($val2['battery']));
$_titlearr=array();
$_batteryarr=array();
foreach($titlearr as $_v){
	$_v=trim($_v);
	if(!empty($_v))$_titlearr[]=$_v;
}
foreach($batteryarr as $_v){
	$_v=trim($_v);
	if(!empty($_v))$_batteryarr[]=$_v;
}
$titlearr =$_titlearr;
$batteryarr =$_batteryarr;
unset($_titlearr);unset($_batteryarr);

					$_bb[$k]['total']=$_bb[$k]['total']+count($titlearr)+count($batteryarr);
					$_bb[$k]['content']=$_bb[$k]['content']+','+$val2['title']+','+$val2['battery']+',';
				}
			}

			$this->assign('a',$_bb);
			$this->assign('count', $count);
			$this->display();	
		}

	}



	public function Createbrand(){
		
		if(isset($_POST['dosubmit'])) {
			if($_POST['t']){
				C('HTML_FILE_SUFFIX','.html');
				if(false === strripos($_POST['t'],'.')){$t=$_POST['t'];}else{$tarr = split ('[.]', $_POST['t']);$t=$tarr[0];}
				$M=M('productadmin');
				$a=$M->select();
				foreach($a as $k=>$val){$b[$k]=strtolower($val['brand']);}
				$a=array_unique($b);
				unset($k);unset($val);unset($b);
				$limitstr=array(
					array(array('\\',':','*','?','"','<','>','|',' '),'-'),
					array(array('/'),'_'),
				);
				set_time_limit (0);
				if(!empty($_POST['b'])){$a=array(trim($_POST['b']));}
				foreach($a as $v){
					$battery_arr=array();
					$series_arr=array();
					$brandarr=$M->field('title,battery')->where('brand="'.$v.'"')->select();

						$vv_low=strtolower($v);
						$vv_filename=strtolower(trim($v));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}
						$varr['title']=$v;
						$varr['title1']=$vv_low;
						$varr['filename']=$vv_filename;
						$this->assign('v', $varr);
					foreach($brandarr as $val){
						if(!empty($val['title'])){
							$titlearr = split ('[,]', $val['title']);
							$series_arr=array_merge($series_arr,$titlearr);
						}
						if(!empty($val['battery'])){
							$batteryarr = split ('[,]', $val['battery']);
							$battery_arr=array_merge($battery_arr,$batteryarr);
						}
					}
					$seriesarr=$series_arr;$batteryarr=$battery_arr;
					unset($series_arr);unset($battery_arr);
					$i=0;
					foreach($seriesarr as $vv){
						$vv_low=strtolower($vv);
						$vv_filename=strtolower(trim($vv));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}
						$series_arr[$i]['title']=$vv;
						$series_arr[$i]['title1']=$vv_low;
						$series_arr[$i]['filename']=$vv_filename;
						$i++;
					}
					$i=0;
					foreach($batteryarr as $vv){
						$vv_low=strtolower($vv);
						$vv_filename=strtolower(trim($vv));
						foreach($limitstr as $li){
							foreach($li[0] as $li2){$vv_filename=strtr($vv_filename,$li2,$li[1]);}
						}
						$battery_arr[$i]['title']=$vv;
						$battery_arr[$i]['title1']=$vv_low;
						$battery_arr[$i]['filename']=$vv_filename;
						$i++;
					}

					$this->assign('series_arr', $series_arr);
					$this->assign('battery_arr', $battery_arr);
					$this->buildHtml($varr['filename'],'./Html/','Home/'.$t);

				}
			}

			$this->ajaxReturn(1,'ok', '1');
		}else{
			$type=  'html';
			$path='./Yourphp/Tpl/Default/Home/';;
			$files = dir_list($path,$type);
			foreach ($files as $key=>$file){
				$filename = basename($file);
				$templates[$key]['filename'] = $filename;
			}
			$this->assign ( 'templates',$templates );
			$M=M('productadmin');
			$a=$M->select();
			foreach($a as $k=>$val){$b[$k]=strtolower($val['brand']);}
			$a=array_unique($b);
			unset($k);unset($val);unset($b);
			$count=count($a);
			$this->assign('a',$a);
			$this->assign('count',$count);
			$this->display();	
		}

	}


	public function showhtml(){
		$type=  'html';
		$path='./Html/' ;
		$files = dir_list($path,$type);
		foreach ($files as $key=>$file){
			$filename = basename($file);
			$templates[$key]['value'] =  substr($filename,0,strrpos($filename, '.'));
			$templates[$key]['filename'] = $filename;
			$templates[$key]['filepath'] = $file;
			$templates[$key]['filesize']=byte_format(filesize($file));
			$templates[$key]['filemtime']=filemtime($file);
			$templates[$key]['ext'] = strtolower(substr($filename,strrpos($filename, '.')-strlen($filename)));
		}
		if($_GET['do']=='del'){
			foreach($templates as $v){
				unlink('Html/'.$v['filename']);
			}
		}else{
			$this->assign ( 'count',count($templates) );
			$this->assign ( 'templates',$templates );
		}
        $this->display ();
	}




	public function stateflag(){
		$_SESSION['htmlfile']=3;
		$this->ajaxReturn(1,$_SESSION['htmlfile'], '1');	
//		unset($_SESSION['htmlfile']);
	}
}
?>